/**
 * 
 */
package com.scpii.universal.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.dao.EnterpriseDao;
import com.scpii.universal.hibernate.dao.TypeDao;
import com.scpii.universal.hibernate.entity.EnterpriseTab;
import com.scpii.universal.hibernate.entity.TypeTab;
import com.scpii.universal.service.EnterpriseService;
import com.scpii.universal.util.ImgAndFilePath;
import com.scpii.universal.util.StreamUtil;
import com.scpii.universal.util.UtilTool;

/**
 * genericWeb
 * 
 * @author weny 2012-7-8下午09:42:00
 */
@Service
public class EnterpriseServiceImpl extends
		BaseServiceImpl<EnterpriseTab, Integer> implements EnterpriseService {
	@Resource
	private EnterpriseDao enterpriseDao;

	@Resource
	private TypeDao typeDao;

	@Resource
	public void setBaseDao(EnterpriseDao enterpriseDao) {
		super.setBaseDao(enterpriseDao);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.scpii.universal.service.EnterpriseService#getNextEnterpId()
	 */
	@Override
	public Integer getNextEnterpId() {
		Map<String, Object> argsMap = new HashMap<String, Object>();
		// TODO Auto-generated method stub
		Long enterpId = enterpriseDao
				.sqlGetNum(
						" select max(e.int_enterp_id)+1 from enterprise_tab e",
						argsMap);
		return Integer.getInteger(enterpId.toString());
	}

	@Override
	public List<TypeTab> getEnterType() {
		Map<String, Object> argsMap = new HashMap<String, Object>();
		String hql = " from TypeTab t where t.intParentTypeId=:intParentTypeId";
		argsMap.put("intParentTypeId", GenericConstants.ENTER_TYPE_ID);
		return typeDao.hqlGetList(hql, argsMap);
	}

	@Override
	public List<EnterpriseTab> getEnterListByTypeId(Integer intTypeId) {
		Map<String, Object> argsMap = new HashMap<String, Object>();
		String hql = " from EnterpriseTab t where t.intEnterpTradeType=:intEnterpTradeType";
		argsMap.put("intEnterpTradeType", intTypeId);
		return enterpriseDao.hqlGetList(hql, argsMap);
	}

	/**
	 * 企業注册
	 * 
	 * @param tab
	 * @param myFile
	 * @author zhangqiang 2012-8-2下午02:48:03
	 */
	@Override
	public void updateEnter(EnterpriseTab tab, File myFile,
			String realPath) {

		// 上传基本 信息
		EnterpriseTab enterpriseInfo = new EnterpriseTab();
		enterpriseInfo.setIntEnterpTradeType(tab.getIntEnterpTradeType());
		enterpriseInfo.setVarEnterpAddress(tab.getVarEnterpAddress());
		enterpriseInfo.setVarEnterpLegalPerson(tab.getVarEnterpLegalPerson());
		enterpriseInfo.setVarEnterpEmail(tab.getVarEnterpEmail());
		enterpriseInfo.setVarEnterpPhonenum(tab.getVarEnterpPhonenum());
		enterpriseInfo.setVarEnterpName(tab.getVarEnterpName());
		// enterpriseInfo.setIntEnterpId(enterpId);
		String dateString = UtilTool.dateToString(new Date());
		enterpriseInfo.setDatUpdateDatetime(dateString);
		enterpriseInfo.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
		enterpriseDao.save(enterpriseInfo);

		ImgAndFilePath.createEnterWork(realPath,
				enterpriseInfo.getIntEnterpId());

		if (myFile != null) {

			String imgAbsolutePath = ImgAndFilePath.ENTERP_WORK_DIR + "_"
					+ enterpriseInfo.getIntEnterpId() + File.separator;

			StreamUtil.uploadActiveImgs(myFile, realPath + imgAbsolutePath
					+ enterpriseInfo.getVarEnterpName() + ".png");
			String filePath = getServerUrl() + imgAbsolutePath
					+ enterpriseInfo.getVarEnterpName() + ".png";
			enterpriseInfo.setVarEnterpLicenseImgUrl(filePath);
			enterpriseDao.update(enterpriseInfo);
		}
	}

}
